﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace TC
{
    public class BTGraphExport : ITranslate<BTGraph, editor.cfg.behaviourtree.BehaviourTree>
    {
        public static Dictionary<Type, TranslateTree> translate = new Dictionary<Type, TranslateTree>()
        {
            {typeof(Service), new Service_tree() },
            {typeof(MoveTo), new MoveTo_tree() },
            {typeof(Sequence), new Sequence_tree() },
            {typeof(Selector), new Selector_tree() },
            {typeof(SimpleParallel), new SimpleParallel_tree() },
            {typeof(IfElse), new IfElse_tree() },
            {typeof(SwitchInt), new Switch_tree() },
        };

        public editor.cfg.behaviourtree.BehaviourTree OnTranslate(BTGraph t)
        {
            editor.cfg.behaviourtree.BehaviourTree re = new editor.cfg.behaviourtree.BehaviourTree();
            re.name = t.name;
            re.root = translate[t.root.GetType()].OnTranslate(t.root);
            
            foreach(var i in t.nodes)
            {
                if(!(i is BTNode))
                {
                    re.blueprintNodes.Add(FlowGraphExport.translate[i.GetType()].OnTranslate(i));
                }
            }
            
            foreach(var i in t.vars)
            {
                if(i.type == VarType.Int)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.IntVar() { defaultValue = (int)i.value});
                }
                else if(i.type == VarType.Float)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.FloatVar() { defaultValue = (float)i.value });
                }
                else if (i.type == VarType.Long)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.LongVar() { defaultValue = (long)i.value });
                }
                else if (i.type == VarType.String)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.StringVar() { defaultValue = (string)i.value });
                }
                else if (i.type == VarType.Bool)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.BoolVar() { defaultValue = (bool)i.value });
                }
                else if (i.type == VarType.Vector3)
                {
                    Vector3 v = (Vector3)i.value;
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.Vector3Var() { defaultValue = new editor.cfg.map.Vector3() { x = v.x, y = v.y, z = v.z } });
                }
                else if(i.type == VarType.GameObject)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.GameObjectVar());
                }
                else if (i.type == VarType.Blurprint)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.BlueprintVar());
                }
                else if (i.type == VarType.IntArray)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.IntArray() { defaultValue = (List<int>)i.value});
                }
                else if (i.type == VarType.FloatArray)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.FloatArray() { defaultValue = (List<float>)i.value });
                }
                else if (i.type == VarType.LongArray)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.LongArray() { defaultValue = (List<long>)i.value });
                }
                else if (i.type == VarType.StringArray)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.StringArray() { defaultValue = (List<string>)i.value });
                }
                else if (i.type == VarType.BoolArray)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.BoolArray() { defaultValue = (List<bool>)i.value });
                }
                else if (i.type == VarType.Vector3Array)
                {
                    editor.cfg.blueprint.Vector3Array tmp = new editor.cfg.blueprint.Vector3Array();
                    List<Vector3> ls = i.value as List<Vector3>;
                    foreach(var j in ls)
                    {
                        tmp.defaultValue.Add(new editor.cfg.map.Vector3() { x = j.x, y = j.y, z = j.z });
                    }
                    re.blackBoard.Add(i.name, tmp);
                }
                else if(i.type == VarType.GameObjectArray)
                {
                    re.blackBoard.Add(i.name, new editor.cfg.blueprint.GameObjectArray());
                }
            }

            return re;
        }
    }
}
